﻿using System;
using System.Collections.Generic;
using System.Text;

using TRPOP.Config;
using TRPOP.Common;

using TRPOP.CMS.Plugin;
using TRPOP.CMS.Model;


namespace TRPOP.CMS.Web.UI
{
    public class BaseAdmin : BasePage
    {

        #region 方法重载

        /// <summary>
        /// 重载 OnInit 验证用户权限
        /// </summary>
        protected override void OnInit(EventArgs e)
        {
            if (this.DesignMode)
            {
                base.OnInit(e);
                return;
            }
            CheckUserRight();

            /*
             * 代码开始  2011.01.08 李城  权限的验证
            */
            SubPageInit();
            CheckUserPermission();
            /*
             * 代码结束  2011.01.08 李城
            */

            /*
             * 代码开始  2011.06.10 李城  修改标题,把企业名称加到标题上
            */
            Page.Title += " -- " + TRPOP.CMS.Plugin.VersionLimit.strEnterpriseName;
            /*
             * 代码结束  2011.06.10 李城
            */
            base.OnInit(e);
        }

        /// <summary>
        /// 动态给每个admin管理页面增加标签
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            //防止搜索引擎搜索到
            System.Web.UI.HtmlControls.HtmlMeta meta = new System.Web.UI.HtmlControls.HtmlMeta();
            meta.Name = "robots";
            meta.Content = "nofollow";
            this.Header.Controls.Add(meta);

            /*此处加入下面的动态增加的代码*/
            base.OnLoad(e);
        }
        #endregion

        #region 子页面继承的方法  当前用于页面权限验证
        /*
         * 代码开始  2011.01.08 李城
        */
        protected PermissionKeys.PermissionEnum m_SubPagePermission = PermissionKeys.PermissionEnum.NullKey;
        protected string m_strPagePermissionExtraKey = "";
        /// <summary>
        /// 子页面初始化
        /// </summary>
        protected virtual void SubPageInit()
        {

        }
        /// <summary>
        /// 验证用户权限码 
        /// </summary>
        /// <param name="oPermissionKeys"></param>
        /// <returns></returns>
        protected bool CheckUserPermission()
        {
            if (m_strPagePermissionExtraKey.IsEmpty() == false)
            {
                return AlertForNoPermission(m_IAdminUser.CheckUserPermission(m_strPagePermissionExtraKey));
            }
            return AlertForNoPermission(m_IAdminUser.CheckUserPermission(m_SubPagePermission));
        }
        /*
         * 代码结束  2011.01.08 李城
        */
        #endregion

        #region 页面公用变量

        /// <summary>
        /// 后台页面title
        /// </summary>
        protected string m_strBaseAdminPageName = TRPOP.CMS.Plugin.VersionLimit.strEnterpriseName + " -- " + SystemInfoHelper.getSystemInfo().CMSAdminPageSuffixName;

        /// <summary>
        /// 系统数据存储时用到的分隔符
        /// </summary>
        protected string m_interval = "|";
        /// <summary>
        /// 信息列表查询排序方式
        /// </summary>
        public const string m_ContentDisplayOrderBY = " PubStatus desc,TopWeight desc,PubTime desc,DisplayOrder desc ";

        #endregion

        #region 用户信息相关操作

        /// <summary>
        /// 用户插件的程序集
        /// </summary>
        protected TRPOP.CMS.Plugin.IAdminUser m_IAdminUser
        {
            get
            {
                return IAdminUserAccess.GetInstance();
            }
        }

        /// <summary>
        /// 当前用户信息
        /// </summary>
        protected TRPOP.CMS.UserManage.AdminUserInfo mUserBaseInfo
        {
            get
            {
                return m_IAdminUser.getUserBaseInfo() as TRPOP.CMS.UserManage.AdminUserInfo;
            }
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="strUsername"></param>
        /// <param name="strPWD"></param>
        /// <returns></returns>
        protected bool AdminUserLogin(string strUsername, string strPWD)
        {
            return m_IAdminUser.AdminLogin(strUsername, strPWD);
        }
        /// <summary>
        /// 检测用户登录状态 用于登录页面
        /// </summary>
        /// <returns></returns>
        protected bool CheckifLogin()
        {
            return m_IAdminUser.CheckLogin();
        }

        /// <summary>
        /// 检测用户信息 用于普通页面
        /// </summary>
        protected void CheckUserRight()
        {
            if (m_IAdminUser.CheckLogin() == false && RequestHelper.GetCurrentUrl().Contains("login.aspx") == false)
            {
                Response.Redirect(
                                       SystemConst.SystemAdminIndexPage +
                                       (SystemConst.SystemAdminIndexPage.EndsWith("/") ? "" : "/") +
                                       "login.aspx", true);
            }
        }
        /// <summary>
        /// 用户添加的时间
        /// </summary>
        protected DateTime GetAdminAddTime
        {
            get
            {
                return DateTime.Now;
            }
        }

        /// <summary>
        ///通过用户Id 获取用户名
        /// </summary>
        /// <param name="adminId"></param>
        /// <returns></returns>
        protected string GetAdminNameById(int intadminId)
        {
            return m_IAdminUser.GetAdminNameById(intadminId);
        }
        /// <summary>
        /// 根据父ID获取所有的权限码集合
        /// </summary>
        /// <param name="intParentid"></param>
        /// <returns></returns>
        protected List<TRPOP.CMS.UserManage.UserPermissionInfo> GetPermissionListByParentId(int intParentid)
        {
            List<TRPOP.CMS.UserManage.UserPermissionInfo> oList = new List<UserManage.UserPermissionInfo>();
            foreach (object objTemo in m_IAdminUser.GetPermissionListByParentId(intParentid))
            {
                oList.Add(objTemo as TRPOP.CMS.UserManage.UserPermissionInfo);
            }
            return oList;
        }
        /// <summary>
        /// 获取当前用户可操作的所有节点。
        /// 格式如下：1,2,3,4,5,6 。 如果全部，则为all。
        /// </summary>
        /// <returns></returns>
        protected string GetEnabledNodeIds()
        {
            return m_IAdminUser.GetEnabledNodeIds();
        }
        /// <summary>
        /// 是否对指定结点有权限
        /// </summary>
        /// <param name="intNodeId"></param>
        /// <returns></returns>
        protected bool CheckIsHavePermission(int intNodeId)
        {
            string strEnableIDs = m_IAdminUser.GetEnabledNodeIds().Trim().ToLower();
            if (strEnableIDs == "all")
                return true;
            else if (("," + strEnableIDs + ",").Contains("," + intNodeId + ","))
                return true;
            return false;
        }

        #endregion

        #region 信息操作弹出窗口
        /// <summary>
        /// 操作提示 仅仅提示
        /// </summary>
        /// <param name="enmResult"></param>
        protected void SubmitAlert(bool blnIsOK)
        {
            if (blnIsOK)
            {
                base.Alert("操作成功");
                return;
            }
            base.Alert("操作失败");
        }


        /// <summary>
        /// 操作提示 仅仅提示
        /// </summary>
        /// <param name="enmResult"></param>
        protected void SubmitAlert(EnumSubmitResult enmResult)
        {
            switch (enmResult)
            {
                case EnumSubmitResult.Success:
                    base.Alert("操作成功");
                    break;
                case EnumSubmitResult.Failed:
                    base.Alert("操作失败");
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 操作提示: 提示并且刷新父窗体
        /// </summary>
        /// <param name="enmResult"></param>
        protected void SubmitAlertAndParReset(EnumSubmitResult enmResult)
        {
            switch (enmResult)
            {
                case EnumSubmitResult.Success:
                    ExecuteJsWidthEnd("alert('操作成功');parent.reset();");
                    break;
                case EnumSubmitResult.Failed:
                    base.Alert("操作失败");
                    break;
                default:
                    break;
            }
        }
        /// <summary>
        /// 提示并跳转到新的页面
        /// </summary>
        /// <param name="enmResult"></param>
        /// <param name="newUrl"></param>
        protected void SubmitAlertAndParNewLocation(EnumSubmitResult enmResult, string newUrl)
        {
            switch (enmResult)
            {
                case EnumSubmitResult.Success:
                    ExecuteJsWidthEnd("alert('操作成功');parent.location.href='" + newUrl + "';");
                    break;
                case EnumSubmitResult.Failed:
                    base.Alert("操作失败");
                    break;
                default:
                    break;
            }
        }
        /// <summary>
        /// 提示并跳转到新的页面
        /// </summary>
        /// <param name="enmResult"></param>
        /// <param name="newUrl"></param>
        protected void SubmitAlertAndThisNewLocation(EnumSubmitResult enmResult, string newUrl)
        {
            switch (enmResult)
            {
                case EnumSubmitResult.Success:
                    ExecuteJsWidthEnd("alert('操作成功');window.location.href='" + newUrl + "';");
                    break;
                case EnumSubmitResult.Failed:
                    base.Alert("操作失败");
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 删除信息后的提示
        /// </summary>
        /// <param name="enmResult"></param>
        protected void SubmitAlertDelete(EnumSubmitResult enmResult)
        {
            switch (enmResult)
            {
                case EnumSubmitResult.Success:
                    base.Alert("删除成功");
                    break;
                case EnumSubmitResult.Failed:
                    base.Alert("删除失败");
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 提示并跳转到新的页面
        /// </summary>
        /// <param name="enmResult"></param>
        /// <param name="newUrl"></param>
        protected void SubmitAlertAndThisNewLocation(bool enmResult, string newUrl)
        {
            if (enmResult)
            {
                ExecuteJsWidthEnd("alert('操作成功');window.location.href='" + newUrl + "';");
                return;
            }
            base.Alert("操作失败");
        }

        /// <summary>
        /// 删除信息后的提示
        /// </summary>
        /// <param name="enmResult"></param>
        protected void SubmitAlertDelete(bool enmResult)
        {
            if (enmResult)
            {
                base.Alert("删除成功");
                return;
            }
            base.Alert("删除失败");
        }

        /// <summary>
        /// 根据bool值判断是否对当前页面有权限
        /// </summary>
        /// <param name="IsHavePermission">是否有权限</param>
        protected bool AlertForNoPermission(bool IsHavePermission)
        {
            if (IsHavePermission)
                return true;
            base.ExecuteJsWidthEnd("alert('您没有权限进入该页面');");
            return false;
        }
        /// <summary>
        /// 根据是否溢出判断是否有权限添加结点
        /// </summary>
        /// <param name="IsOvewrFlow"></param>
        protected void AlertForNoPermissionAddNode(bool IsOvewrFlow)
        {
            if (IsOvewrFlow == false)
                return;
            base.ExecuteJsWidthEnd("alert('当前结点数量已超过最大限制!');");
        }
        /// <summary>
        /// 根据是否溢出判断是否有权限添加PSN
        /// </summary>
        /// <param name="IsOvewrFlow"></param>
        protected void AlertForNoPermissionAddPSN(bool IsOvewrFlow)
        {
            if (IsOvewrFlow == false)
                return;
            base.ExecuteJsWidthEnd("alert('当前PSN数量已超过最大限制!');");
        }
        /// <summary>
        /// 根据是否溢出判断是否有权限添加Content
        /// </summary>
        /// <param name="IsOvewrFlow"></param>
        protected void AlertForNoPermissionAddContent(bool IsOvewrFlow)
        {
            if (IsOvewrFlow == false)
                return;
            base.ExecuteJsWidthEnd("alert('当前信息数量已超过最大限制!');");
        }
        /// <summary>
        /// 判断是否有权限操作
        /// </summary>
        /// <param name="IsOvewrFlow"></param>
        protected void AlertForNoPermissionControl(bool IsHavePermission)
        {
            if (IsHavePermission)
                return;
            base.ExecuteJsWidthEnd("alert('当前版本没有此功能!');");
        }
        /// <summary>
        /// 判断给定的对象是否为空，如果为空则弹出给定的信息，并返回true，否则返回true
        /// </summary>
        /// <param name="strMessage"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        protected bool CheckIfEmptyAndAlert(string strMessage, params object[] args)
        {
            foreach (object obj in args)
            {
                if (obj == null || obj.ToString().IsEmpty())
                {
                    Alert(strMessage);
                    return true;
                }
            }
            return false;
        }
        #endregion

        #region 页面传递通用方法 试图操作方法

        /// <summary>
        /// 针对页面传递的值，保存到视图里
        /// </summary>
        /// <param name="ResquestStr"></param>
        /// <returns></returns>
        protected string InitRequestViewState(string ResquestStr)
        {
            string oResquestStr = RequestHelper.RequestString(ResquestStr);
            ViewState[ResquestStr] = oResquestStr;
            return ViewState[ResquestStr].ToString();
        }

        /// <summary>
        /// 获取页面视图
        /// </summary>
        /// <param name="ResquestStr"></param>
        /// <returns></returns>
        protected string GetRequestViewState(string ResquestStr)
        {
            if (ViewState[ResquestStr] != null)
            {
                return ViewState[ResquestStr].ToString();
            }
            else
                return "";
        }
        /// <summary>
        /// 初始化页面试图
        /// </summary>
        /// <param name="name"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        protected object InitPageViewState(string name, object obj)
        {
            ViewState[name] = obj;
            return obj;
        }
        /// <summary>
        /// 获得页面试图
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        protected object GetPageViewState(string name)
        {
            return ViewState[name];
        }


        #endregion

    }
}
